/**
 * 通用帮助函数
 *  @func ajaxRequest
 *  @func getElement
 *  @func showLoader
 *  @func hiddenLoader
 */
var Common = {
	ajaxRequest: function(url, callback, ctx){
		new Ajax.Request(url, {
			method: 'post',
		  onSuccess: function(response) { // only interest in success
			if (200 == response.status)
				callback.call(ctx, response.responseText.evalJSON());
		  }
		});
	},
	getElement: function(Id){
		return document.getElementById(Id);
	},
	//弹出加载图标层
	showLoader: function(){
		var alertWin = this.getElement("mod-ajaxLoader");
		alertWin.style.display = "block";

		var mybg = document.createElement("div");
		mybg.setAttribute("id", "alert_bg");
		mybg.style.background = "black";
		mybg.style.width = "100%";
		mybg.style.height = 150 + "px";
		mybg.style.position = "absolute";
		mybg.style.overflow = "hidden";
		mybg.style.top = "0";
		mybg.style.left = "0";
		mybg.style.zIndex = "2500";
		mybg.style.opacity = "0.5";
		
		//var topWin = window.top.this.getElement("iframe").contentWindow;
		var body = this.getElement("mod-myBody");
		body.appendChild(mybg);
		body.style.overflow = "hidden";

	},
	//隐藏loader图标
	hiddenLoader: function() {
		var alertWin = this.getElement("mod-ajaxLoader");
		var mybg = this.getElement("alert_bg");
		alertWin && (alertWin.style.display = "none");
		if(mybg){
			//var topWin = window.top.this.getElement("iframe").contentWindow;
			var body = this.getElement("mod-myBody");
			body.removeChild(mybg);
		}
	}
};

/**
 * 微信验证
 * @func checkSignature
 */
var WeixinSignature = {
	checkSignature: function(){
		this.ajaxRequest('/api/ajax/getWeixinSignatureData', this.checkSignatureCallback);
	},

	checkSignatureCallback: function(response){
		this.wxappid=response.appId;
		this.wxtimestamp=response.timestamp;
		this.wxnonceStr=response.nonceStr;
		this.wxsignature=response.signature;
		
		this.closeWeixinMenu();
		this.initWeixinApi();
	},

	closeWeixinMenu: function(){		
		wx.ready(function () {
			wx.hideOptionMenu();
		});	
	},
	initWeixinApi: function(){
		wx.config({
	      debug: false,
	      appId: this.wxappid,
	      timestamp: this.wxtimestamp,
	      nonceStr: this.wxnonceStr,
	      signature: this.wxsignature,
	      jsApiList: [
	        'onWXDeviceBindStateChange',
	        'onWXDeviceStateChange',
	        'onScanWXDeviceResult',
	        'onReceiveDataFromWXDevice',
	        'onWXDeviceBluetoothStateChange',
	        'openWXDeviceLib',
	        'closeWXDeviceLib',
	        'getWXDeviceInfos',
	        'getWXDeviceTicket',
	        'setSendDataDirection',
	        'startScanWXDevice',
	        'stopScanWXDevice',
	        'connectWXDevice',
	        'disconnectWXDevice',
	        'sendDataToWXDevice',
	        'scanQRCode',
	        'configWXDeviceWiFi',
	        'onMenuShareTimeline',
	        'onMenuShareAppMessage',
	        'onMenuShareQQ',
	        'onMenuShareWeibo',
	        'hideMenuItems',
	        'showMenuItems',
	        'hideAllNonBaseMenuItem',
	        'showAllNonBaseMenuItem',
	        'translateVoice',
	        'playMusic',
	        'startRecord',
	        'stopRecord',
	        'onRecordEnd',
	        'playVoice',
	        'pauseVoice',
	        'stopVoice',
	        'uploadVoice',
	        'downloadVoice',
	        'chooseImage',
	        'previewImage',
	        'uploadImage',
	        'downloadImage',
	        'getNetworkType',
	        'openLocation',
	        'getLocation',
	        'hideOptionMenu',
	        'showOptionMenu',
	        'closeWindow',
	        'scanQRCode',
	        'chooseWXPay',
	        'getLatestAddress',
	        'editAddress',
	        'openProductSpecificView',
	        'addCard',
	        'chooseCard'
	      ]
	  });
	}
}